package com.lightsaber.trade.core.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.BaseEntity;

/**
 * 泛型Service, 必须实现 getDao()方法
 * 
 * 
 * @author LiuJian
 * 
 * @param <T>
 *            A entity which extends BaseEntity
 * @param <PK>
 *            The type of primary key of the entity
 */
@Transactional
public abstract class BaseServiceImpl<T extends BaseEntity, PK extends Serializable> implements
        BaseService<T, PK> {

    /**
     * Get the DAO which you have inject into yourself Service as it's property.
     * 
     * @author LiuJian
     * @return
     */
    protected abstract BaseDao<T, PK> getDao();

    /*
     * (non-Javadoc)
     * 
     * @see com.accentrix.vtc.hpp.service.IBaseService#delete(T)
     */
    @Override
    public void delete(final T t) {
        getDao().delete(t);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.accentrix.vtc.hpp.service.IBaseService#delete(PK)
     */
    @Override
    public void delete(final PK id) {
        getDao().delete(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.accentrix.vtc.hpp.service.IBaseService#getAll()
     */
    @Override
    public List<T> getAll() {
        return getDao().getAll();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.accentrix.vtc.hpp.service.IBaseService#get(PK)
     */
    @Override
    public T get(final PK id) {
        return getDao().get(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.accentrix.vtc.hpp.service.IBaseService#save(T)
     */
    @Override
    public void save(final T t) {
        getDao().save(t);
    }

    /**
     * 可以把 瞬时态(Transient)、脱管态(Detached)变成 持久态(Persistent)
     */
    @Override
    public void update(final T t) {
        getDao().update(t);
    }

    @Override
    public Page<T> findPage(final Page<T> page, final List<PropertyFilter> filters) {
        return getDao().findPage(page, filters);
    }

}
